package com.ghxxzx.daglcustom.controller.task;

import com.ghxxzx.daglcustom.utils.ActivitiTools;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tmsps.ne4spring.utils.ChkUtil;

import java.util.List;
import java.util.Map;

@Controller
public class HastodoTasksController {

	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private HistoryService historyService;

	@RequestMapping("/hastodoTasksList")
	@ResponseBody
	public List<Map<String, Object>> HastodoTasksList(String name) {
		// 创建查询对象
		HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
		if (ChkUtil.isNotNull(name)) {
			historicTaskInstanceQuery.processDefinitionName("%" + name + "%");
		}
		// 设置查询条件
		// 根据任务的办理人查询
		// historicTaskInstanceQuery.taskAssignee(assignee);
		List<HistoricTaskInstance> list = historicTaskInstanceQuery.finished().list();

		/*
		 * List<Map<String, Object>> listEnd =
		 * ActivitiTools.turnHistoricTaskInstance(list);
		 * 
		 * // 分页时用,非分页慎用 ProcessInstanceQuery instanceQuery =
		 * runtimeService.createProcessInstanceQuery(); for (Map<String, Object> map :
		 * listEnd) { String processDefinitionId = (String)
		 * map.get("processDefinitionId"); if (processDefinitionId != null) {
		 * List<ProcessInstance> processInstanceList =
		 * instanceQuery.processDefinitionId(processDefinitionId).list(); if
		 * (processInstanceList != null) { for (ProcessInstance processInstance :
		 * processInstanceList) { map.put("processInstanceName",
		 * processInstance.getName()); } } } }
		 */
		return ActivitiTools.turnHistoricTaskInstance(list);
	}

	/**
	 * 流程监控(查询已经发起的流程实例以及已经执行完成的流程实例)
	 * @return
	 */
	@RequestMapping("/queryHistoricInstance")
	@ResponseBody
	public List<HistoricProcessInstance> queryHistoricInstance(String name){
		//根据条件查询所有的流程实例
		HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery();
		if (ChkUtil.isNotNull(name)) {
			historicProcessInstanceQuery.processInstanceNameLike("%"+name+"%");
		}
		List<HistoricProcessInstance> list = historicProcessInstanceQuery.orderByProcessInstanceStartTime().asc().list();
		//判断流程实例是否走完
		for(int i = 0;i<list.size();i++) {
			ProcessInstanceQuery createProcessInstanceQuery = runtimeService.createProcessInstanceQuery();
			ProcessInstanceQuery processInstanceId = createProcessInstanceQuery.processInstanceId(list.get(i).getId());
			ProcessInstance singleResult = processInstanceId.singleResult();
			if(singleResult==null){
				list.get(i).setLocalizedDescription("已完成");
			}else{
				list.get(i).setLocalizedDescription("进行中");
			}
		}
		return list;
	}
}
